/**
 * 画线类
 * 
 * 调用   setline.getinstance().sl((起点)(终点)(线编号))来画线
 * 
 * 
 * 使用以下代码初始化
 * 
        let sl = setline.getinstance();
        if (!sl.parent) {
            this.addChild(sl);
        }

 */

class setline extends eui.Component {

	public constructor() {
		super();
		this.createChildren();
	}

	public createChildren() {
		super.createChildren();

		this.initshpArr();
	}

	private static __instance: setline;

	/**画线 .sl()  画圆 .sc()  清线  .cl() */
	public static getinstance() {
		if (this.__instance == null) {
			this.__instance = new setline();
		}
		return this.__instance
	}
	/** 施工中
	 * 
	 *    给出 起点 方向  碰撞体组 画出线段 
	 * 
	 * */
	public setSubline(_r, angleSpeed, angleNum, huadongAngle, holeR, posArr, curBall, ballArr) {

		/**递增基数 */
		var v: number = 2;
		// console.log(huadongAngle);
		var vx: number = 0;
		var vy: number = 0;

		/**求发射方向 */
		if (angleNum == 1) {
			vx = v * Math.sin(angleSpeed);
			vy = -v * Math.cos(angleSpeed);
		}
		else if (angleNum == 2) {
			vx = v * Math.cos(angleSpeed);
			vy = v * Math.sin(angleSpeed);
		}
		else if (angleNum == 3) {
			vx = v * Math.sin(angleSpeed);
			vy = -v * Math.cos(angleSpeed);
		}
		else if (angleNum == 4) {
			vx = -v * Math.cos(angleSpeed);
			vy = v * Math.sin(angleSpeed);
		}
		/**清空上一次绘制的瞄准线 */
		this.clin0();
		/**瞄准线起点 */
		var px: number = curBall.nextX;
		var py: number = curBall.nextY;


		var is: boolean = false;
		var have: boolean = true;
		/**开始延伸瞄准线	 */
		for (var i: number = 0; i < 1500; i++) {
			px += vx;
			py += vy;

			/**遍历所有彩球 若碰到彩球绘制彩球走向 绘制白球走向 绘制白球碰撞位置 碰到之后is=true */
			var len: number = ballArr.length;
			for (var j: number = 0; j < len; j++) {
				var ballA: NewBall = ballArr[j];
				if (ballA == undefined) { continue; }
				if (ballA.name == "cur") {
					continue;
				}
				var dx: number = ballA.nextX - px;
				var dy: number = ballA.nextY - py;
				var dist: number = Math.sqrt(dx * dx + dy * dy);
				if (dist <= 2 * _r) {
					is = true;

					var a1: any = this.rotationCoord(px, py, huadongAngle);
					var a2: any = this.rotationCoord(ballA.nextX, ballA.nextY, huadongAngle);

					var dx0: number = a1.x - a2.x;
					var dy0: number = a1.y - a2.y;
					var dist0: number = Math.sqrt(dx0 * dx0 + dy0 * dy0);
					var asd: number = Math.abs(dx0 / dist0);
					var num: number = dy0 / Math.abs(dy0);

					var angle: number = Math.atan2(dy, dx);
					var sin: number = Math.sin(angle);
					var cos: number = Math.cos(angle);
					var pos1: egret.Point = this.rotateR(dx, dy, sin, cos, true);

					pos1.x += 70 * asd;

					var pos2: egret.Point = new egret.Point(0, num * (_r + 50 * (1 - asd)));

					var pos3: egret.Point = new egret.Point(0, num * _r);

					var pos1F: any = this.rotateR(pos1.x, pos1.y, sin, cos, false);

					var pos2F: any = this.rotateR(pos2.x, pos2.y, sin, cos, false);

					var pos3F: any = this.rotateR(pos3.x, pos3.y, sin, cos, false);

					setline.getinstance().sl(ballA.nextX, ballA.nextY, px + pos1F.x, py + pos1F.y);
					setline.getinstance().sl(px + pos3F.x, py + pos3F.y, px + pos2F.x, py + pos2F.y);

					break;
				}
			}

			/**彩球遍历结束 若检测到彩球 退出探测 */
			if (is) { break; }


			/**遍历6个球洞 碰到之后have = false*/

			for (var i: number = 0; i < posArr.length; i++) {
				var _hx: number = posArr[i].hx;
				var _hy: number = posArr[i].hy;
				if (px > _hx - holeR && px < _hx + holeR && py > _hy - holeR && py < _hy + holeR) {
					var dx: number = px - _hx;
					var dy: number = py - _hy;
					var dist: number = Math.sqrt(dx * dx + dy * dy);
					if (dist < holeR) {
						have = false;
						break;
					}
				}
			}


			/**探测上下左右四个方向(墙) */
			if (px < posArr[0].x2 + _r) { break; }
			if (px > posArr[2].x2 - _r) { break; }
			if (py < posArr[0].y1 + _r) { break; }
			if (py > posArr[3].y2 - _r) { break; }
		}
		/**瞄准线递增结束 延伸到最长的位置为(px, py)瞄准线画至此处 并在末尾画圆*/

		setline.getinstance().sl(curBall.nextX, curBall.nextY, px, py);
		setline.getinstance().sc(px, py, _r - 2);
	}


	/** */
	private rotateR(x: number, y: number, sin: number, cos: number, reverse: Boolean): egret.Point {
		var result: egret.Point = new egret.Point();
		if (reverse) {
			result.x = x * cos + y * sin;
			result.y = y * cos - x * sin;
		} else {
			result.x = x * cos - y * sin;
			result.y = y * cos + x * sin;
		}
		return result;
	}

	/** */
	public rotationCoord(_x: number, _y: number, __angele) {
		if (__angele == 0) {
			__angele = 0.0001;
		}
		var radian: number = -__angele;
		var a: number = Math.cos(radian) * Math.cos(radian) / Math.sin(radian) + Math.sin(radian);
		var x: number = ((Math.cos(radian) * _x) / Math.sin(radian) - _y) / a;
		var b: number = Math.cos(radian) + Math.sin(radian) * Math.sin(radian) / Math.cos(radian);
		var y: number = ((Math.sin(radian) * _x) / Math.cos(radian) + _y) / b;
		return { x: x, y: y }
	}

	/**根据求终端相对起点的角度 */
	public GetAngel(sx, sy, nx, ny) {
		//起点
		var qx: number = sx;
		var qy: number = sy;
		//终端
		var dx: number = nx;
		var dy: number = ny;
		//距离
		var _a: number = dx - qx;//左右(a>0右)
		var _b: number = dy - qy;//上下(b>0下)
		var _c: number = Math.sqrt((dx - qx) * (dx - qx) + (dy - qy) * (dy - qy));

		var Result = { x: 0, y: 0, rotation: 0, speed: 0, angleNum: 0, huadongAngle: 0, ganLen: 0 };
		//↗
		if (_a >= 0 && _b <= 0) {
			Result.rotation = Math.asin(_a / _c) * 180 / Math.PI - 90;//相对角度
			Result.speed = Math.asin(_a / _c);
			Result.angleNum = 1;
			Result.huadongAngle = Result.speed - Math.PI / 2;
			Result.x = qx - (Result.ganLen) * Math.sin(Math.asin(_a / _c));
			Result.y = qy + (Result.ganLen) * Math.cos(Math.asin(_a / _c));
		}
		//↘
		if (_a > 0 && _b > 0) {
			Result.rotation = (Math.asin(_b / _c)) * 180 / Math.PI;
			Result.speed = Math.asin(_b / _c);
			Result.angleNum = 2;
			Result.huadongAngle = Result.speed;
			Result.x = qx - (Result.ganLen) * Math.cos((Math.asin(_b / _c)));
			Result.y = qy - (Result.ganLen) * Math.sin((Math.asin(_b / _c)));
		}
		//↖
		if (_b < 0 && _a < 0) {
			Result.rotation = Math.asin(_a / _c) * 180 / Math.PI - 90;
			Result.speed = Math.asin(_a / _c);
			Result.angleNum = 3;
			Result.huadongAngle = Result.speed - Math.PI / 2;
			Result.x = qx + (Result.ganLen) * Math.sin(Math.asin(-_a / _c));
			Result.y = qy + (Result.ganLen) * Math.cos(Math.asin(-_a / _c));
		}
		//↙
		if (_a < 0 && _b > 0) {
			Result.rotation = (-(Math.asin(_b / _c) + 90 * Math.PI / 180)) * 180 / Math.PI - 90;
			Result.speed = Math.asin(_b / _c);
			Result.angleNum = 4;
			Result.huadongAngle = Math.PI - Result.speed;
			Result.x = qx + (Result.ganLen) * Math.cos(Math.asin(_b / _c));
			Result.y = qy - (Result.ganLen) * Math.sin(Math.asin(_b / _c));
		}
		return Result;
	}


	/**0号线 */
	private s: egret.Shape;
	/**1号线 */
	private ss: egret.Shape;
	/**2号线 */
	private sss: egret.Shape;
	/**3号线 */
	private ssss: egret.Shape;

	/**初始化 */
	private initshpArr() {
		this.shpArr = [this.s, this.ss, this.sss, this.ssss];
		if (this.s == undefined) {
			this.s = new egret.Shape();
			this.addChild(this.s);
		} if (this.ss == undefined) {
			this.ss = new egret.Shape();
			this.addChild(this.ss);
		} if (this.sss == undefined) {
			this.sss = new egret.Shape();
			this.addChild(this.sss);
		} if (this.ssss == undefined) {
			this.ssss = new egret.Shape();
			this.addChild(this.ssss);
		}
	}

	/**线组 */
	private shpArr: egret.Shape[];

	/**起点(sx,sy) 终点(nx,ny) 线shp*/
	public setline(sx, sy, nx, ny, shp: egret.Shape, color?) {
		shp.graphics.clear();
		if (color == undefined) {

			shp.graphics.lineStyle(3, 0xFF0000);//红色
		} else {
			shp.graphics.lineStyle(3, color);
		}
		shp.graphics.moveTo(sx, sy);
		shp.graphics.lineTo(nx, ny);
		shp.graphics.endFill();
	}


	/**画线 起点(sx,sy) 终点(nx,ny) 线编号*/
	public sl(sx, sy, nx, ny, n?) {
		if (n == undefined) {
			n = 0;
		} else {
			n--;
		}
		if (n < 0) n = 0;
		if (n > this.shpArr.length - 1) n = this.shpArr.length - 1;
		var shp = this.shpArr[n];
		if (n != 0)
			this.cl(shp);
		shp.graphics.lineStyle(3, 0xFF0000);//红色
		shp.graphics.moveTo(sx, sy);
		shp.graphics.lineTo(nx, ny);
		shp.graphics.endFill();
	}
	/**画圆 */
	public sc(x, y, r, n?) {
		if (n == undefined) {
			n = 0;
		} else {
			n--;
		}
		if (n < 0) n = 0;
		if (n > this.shpArr.length - 1) n = this.shpArr.length - 1;
		var shp = this.shpArr[n];
		if (n != 0)
			this.cl(shp);
		shp.graphics.lineStyle(3, 0xFF0000);//红色
		shp.graphics.drawCircle(x, y, r);
		shp.graphics.endFill();

	}

	/**清线 */
	public cl(lin: egret.Shape) {
		lin.graphics.clear();
	}

	/**清除0号线 */
	public clin0() {
		this.s.graphics.clear();
	}
}